package com.example.animationdrawabledemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.util.Log;

public class NewBubbleDrawable2 extends Drawable {

	private static final String TAG = NewBubbleDrawable2.class.getSimpleName();
	Bitmap mutable;
	Paint paint;
	Bitmap mask,bgNor,grain;
	Canvas canvas;
	NinePatchDrawable ninePatchDrawable,maskNinePatchDrawable;
	private Rect rect;
	private BitmapShader bShader;
	public NewBubbleDrawable2(Context context) {
		super();
		// TODO Auto-generated constructor stub
		mask = BitmapFactory.decodeResource(context.getResources(), R.drawable.aio_friend_bg_mask);
		bgNor = BitmapFactory.decodeResource(context.getResources(), R.drawable.aio_friend_bg_nor);
		ninePatchDrawable = new NinePatchDrawable(context.getResources(), bgNor, bgNor.getNinePatchChunk(), new Rect(), null);
		maskNinePatchDrawable = new NinePatchDrawable(context.getResources(), mask, mask.getNinePatchChunk(), new Rect(), null);
		grain =BitmapFactory.decodeResource(context.getResources(), R.drawable.aio_textrue_nor);
		paint = new Paint();
		paint.setFilterBitmap(false);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
		bShader = new BitmapShader(grain,TileMode.REPEAT,TileMode.REPEAT);
		paint.setShader(bShader);
	}
	
	@Override
	protected void onBoundsChange(Rect bounds) {
		super.onBoundsChange(bounds);
		mutable = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.ARGB_8888);
		canvas = new Canvas(mutable);
		rect = bounds;
	}

	private long startTime = 0;
	@Override
	public void draw(Canvas onDrawCanvas) {
		if(rect == null){
			return;
		}
		startTime = System.currentTimeMillis();
		maskNinePatchDrawable.setBounds(rect);
		maskNinePatchDrawable.draw(canvas);
		Log.v(TAG, "1:"+(System.currentTimeMillis()-startTime));
//		createRepeater(getIntrinsicWidth(),getIntrinsicHeight(),grain,canvas,paint);
		canvas.drawRect(rect, paint);
		Log.v(TAG, "2:"+(System.currentTimeMillis()-startTime));
		ninePatchDrawable.setBounds(rect);
		ninePatchDrawable.draw(canvas);
		Log.v(TAG, "3:"+(System.currentTimeMillis()-startTime));
		onDrawCanvas.drawBitmap(mutable, 0, 0, null);
		Log.v(TAG, "4:"+(System.currentTimeMillis()-startTime));
	}

	@Override
	public void setAlpha(int alpha) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setColorFilter(ColorFilter cf) {
		// TODO Auto-generated method stub

	}

	@Override
	public int getOpacity() {
		// TODO Auto-generated method stub
		return 0;
	}
	
	public static void createRepeater(int width,int height,Bitmap src,Canvas canvas,Paint paint) {
		int count = (width + src.getWidth() - 1) / src.getWidth();
		int heightcount = (height + src.getHeight() - 1) / src.getHeight();
		for (int idx = 0; idx < count; ++idx) {
			for(int idx2 = 0; idx2 < heightcount; ++idx2){
				canvas.drawBitmap(src, idx * src.getWidth(), idx2 * src.getHeight(), paint);
			}
		}
	}

}
